package com.example.s12redis.service.impl;

import com.example.s12redis.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Set;

/**
 * @auther lazyfier
 * @date 2022/4/10 15:01
 */

@Service
public class RedisServiceImpl implements IRedisService
{
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean set(String key, Object value)
    {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

    @Override
    public Object get(String key)
    {
        ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    @Override
    public void remove(String key) {

        Set<Serializable> keys = redisTemplate.keys(key);
        if(keys.size()>0)
        {
            redisTemplate.delete(keys);
        }
    }


}
